package com.bkbw.live.battle.map;

import java.awt.Point;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import org.apache.commons.math3.geometry.euclidean.threed.Vector3D;

import com.bkbw.live.battle.util.AStar;
import com.bkbw.live.battle.util.AStar.Node;

public class MapAStarInfo {
	
	private int maxX;
	private int maxZ;
	/**
	 * 地图信息，默认值为0，表示可以通过点（可寻路点）
	 */
	private int[][] data;
	
	private AStar star;
	/**
	 * 创建一个多少行，多少列的地图
	 * @param maxX x方向的最大格子值
	 * @param maxZ z方向的最大格子值
	 */
	public MapAStarInfo(int maxX,int maxZ) {
		super();
		this.maxX = maxX;
		this.maxZ = maxZ;
		this.data = new int[maxX][maxZ];
		star = new AStar();
	}

	public int[][] getData() {
		return data;
	}
	public int getMaxX() {
		return maxX;
	}
	public int getMaxZ() {
		return maxZ;
	}
	
	public boolean canCross(int x, int z) {
		if (data[x][z] == 0) {
			return true;
		}
		return false;
	}
	
	//设置不可通过点
	public void setBarrierPoint(int x,int z,int type){
		if(x<maxX&&z<maxZ&&x>=0&&z>=0){
			if(type>0){
				data[x][z]=type;
			}else{
				data[x][z]=type;
				//throw new RuntimeException("障碍点的type不能是0,因为0表示可以通过点");
			}
		}
	}
	
	//找到路径点
	public List<Vector3D> findPath(Vector3D start,Vector3D end){
		//交给A＊引擎运算
		star.NODES = data;
//		for(int i=getMaxZ()-1;i>0;i--){
//			for(int j=0;j<getMaxX();j++){
//				System.out.print(data[j][i]);
//			}
//			System.out.println();
//		}
		
		AStar.Node startNode = new Node((int)Math.round(start.getX()), (int)Math.round(start.getZ()));
		AStar.Node endNode = new Node((int)Math.round(end.getX()), (int)Math.round(end.getZ()));
		AStar.Node parent = star.findPath(startNode, endNode);
		ArrayList<Vector3D> arrayList = new ArrayList<Vector3D>();
	        while (parent != null) {
	            // System.out.println(parent.x + ", " + parent.y);
	            arrayList.add(new Vector3D(parent.x+0.5,0,parent.y+0.5));
	            parent = parent.parent;
	        }
	    Collections.reverse(arrayList);
		return arrayList;
	}
}
